﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;

namespace DAL
{
    public class OperateDB
    {
        private static SqlConnection DbConnection;//定义sqlconnection 类型的变量
        private static string constr;
        private static SqlConnection conn;
        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlDataAdapter adapter;

        public OperateDB(string ConnectString) //构造函数，当程序创建这个变量的时候，会自动调用构造函数
        {
            DbConnection = new SqlConnection(ConnectString);
           DbConnection.Open();
       }

      //  ~OperateDB()//析构函数，变量没有用之后自动调用这个函数，比如运行结束或者超出变量作用域
        //{
          //  DbConnection.Close();

        //}

        public static SqlConnection MyConnection
        {
            get
            {
                //必须添加引用System.configuartion
                constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                //.Net默认打开数据库连接池
                conn = new SqlConnection(constr);
                conn.Open();
                return conn;
            }
        }

        public static DataTable GetFillData(string cmdTxt, params SqlParameter[] pars)
        {
            DataSet ds = new DataSet();
            using (cmd = new SqlCommand(cmdTxt, DbConnection))
            {
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                using (adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }

        


        public void Execute(string OperationCommand)
        {
            SqlCommand DbCommand = new SqlCommand(OperationCommand, DbConnection);
            DbCommand.ExecuteNonQuery();
        }

        public SqlDataReader Select(string OperationCommand)
        {
            SqlCommand DbCommand = new SqlCommand(OperationCommand, DbConnection);
            DbCommand.ExecuteNonQuery();

            return DbCommand.ExecuteReader();
        }

        //public int Select(string OperationCommand)
        //{
        //    SqlCommand DbCommand = new SqlCommand(OperationCommand, DbConnection);
        //    DbCommand.ExecuteNonQuery();
        //    dr.Read();
        //    int selectRoleIdResult = Convert.ToInt32(dr["RoleId"]);
        //    dr.Close();
        //}


        public object SelectS(string OperationCommand)
        {
            SqlCommand DbCommand = new SqlCommand(OperationCommand, DbConnection);
            DbCommand.ExecuteNonQuery();

            return DbCommand.ExecuteScalar();
        }

        public SqlConnection DbConnectionProperty
        {
            get
            {               
                return DbConnection;
            }
        }
    }
}
